package com.ydzt.fanka.server.zwcl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.ydzt.fanka.server.domain.StatisticsInfo;
import com.ydzt.fanka.server.utils.ByteConvert;
import com.ydzt.fanka.server.utils.DBHelper;
import com.ydzt.fanka.server.utils.JdbcUtils;
import com.ydzt.fanka.server.utils.NetUtil;

public class ZwclStatisticsServer extends Thread{
	static Logger logger = Logger.getLogger(ZwclStatisticsServer.class.getName());
	
	public void run(){
		List<StatisticsInfo> statisticsInfos = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		while(true){
			if(DBHelper.getStatisticsState().equals("START")){
				statisticsInfos = new ArrayList<StatisticsInfo>();
				//将明细写入统计
				String stmtDay = DBHelper.getStmtDay();
				String month = stmtDay.substring(4,6);
				String sqlDetailInfo = "";
				sqlDetailInfo += " select supplier_id ,stmt_day,card_type,trans_type,count(*) times,sum(amount) amount";
				sqlDetailInfo += " from ";
				sqlDetailInfo += " tbl_centre_transaction_" + month + " where stmt_day = '" + stmtDay + "'";
				sqlDetailInfo += " group by supplier_id,stmt_day,card_type,trans_type";
				try {
					conn = JdbcUtils.getConnectionForOracle();
					conn.setAutoCommit(false);
					pstmt = conn.prepareStatement(sqlDetailInfo);
					rs = pstmt.executeQuery();
					while(rs.next()){
						StatisticsInfo statisticsInfo = new StatisticsInfo();
						statisticsInfo.setSupplier_id(rs.getString("supplier_id"));
						statisticsInfo.setStmt_day(rs.getString("stmt_day"));
						statisticsInfo.setCard_type(rs.getInt("card_type"));
						statisticsInfo.setTrans_type(rs.getInt("trans_type"));
						statisticsInfo.setTimes(rs.getLong("times"));
						statisticsInfo.setAmount(rs.getLong("amount"));
						statisticsInfos.add(statisticsInfo);
					}
					
					StringBuilder sBuilder = new StringBuilder();
					for(int i = 0;i < statisticsInfos.size(); i ++){
						sBuilder.append("insert into tbl_centre_transaction_day");
						sBuilder.append("(supplier_id,stmt_day,card_type,trans_type,times,amount) values(");
						sBuilder.append("'" + statisticsInfos.get(i).getSupplier_id() + "',");
						sBuilder.append("'" + statisticsInfos.get(i).getStmt_day() + "',");
						sBuilder.append(statisticsInfos.get(i).getCard_type() + ",");
						sBuilder.append(statisticsInfos.get(i).getTrans_type() + ",");
						sBuilder.append(statisticsInfos.get(i).getTimes() + ",");
						sBuilder.append(statisticsInfos.get(i).getAmount());
						sBuilder.append(")");
						String sqlText = sBuilder.toString();
						sBuilder.delete(0, sBuilder.length());
						try {
							pstmt = conn.prepareStatement(sqlText);
							pstmt.executeQuery();
							conn.commit();
						} catch (SQLException e) {
							logger.error("插入tbl_centre_transaction_day出错!" + e.getMessage());
						}
					}
					conn.setAutoCommit(true);
					DBHelper.setStatisticsState("END");
					logger.info("进行日终步骤---->对明细进行统计完成!");
					
					//明细入统计完成 生成对账报文
					//组对账报文
					//组包头
					int offset = 0;
					byte[] head = new byte[23];
					//两字节 参数类型
					head[0] = 0x30;
					head[1] = 0x01;
					offset += 2;
					//四字节  发送方IP
					byte[] sendByte = new byte[4];
					sendByte = ByteConvert.ipv4Address2BinaryArray(NetUtil.getIp());
					System.arraycopy(sendByte, 0, head, offset, 4);
					//四字节 接收方IP
					@SuppressWarnings("unused")
					byte[] receiverByte = new byte[4];
					
				} catch (SQLException e) {
					logger.error("明细入统计出错:" + e.getMessage());
					try{
						if(conn != null){
							conn.rollback();
							conn.setAutoCommit(true);
						}
					}
					catch(SQLException e2){
					}
				} finally{
					JdbcUtils.freeAll(rs, pstmt, conn);
				}
			}
			else if(DBHelper.getStatisticsState().equals("END")){//正在进行日终 日终所处理的数据还未处理完
				continue;
			}
		}
	}
}
